<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/***************************************************************************
 * 
 * Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
 * 
 **************************************************************************/
 
 
 
/**
 * @todo 文章管理控制器
 * @package controller 
 * @file Posts.php
 * @author lihongliang01@baidu.com
 * @date 2014年11月2日 下午1:23:02
 * 
 *  
 **/
class Posts extends MY_ADMIN_Controller {
	private static $categories = array();
	
	function __construct() {
		parent::__construct();
		$this->loadModel('Category_model', 'Category');
		self::$categories = format_tree($this->Category->getCategory(false));
		$this->loadModel('Post_model', 'Post');
	}
	
	function index() {
		$like = $this->_searchLike($this->Post);
		$where = $this->_searchWhere($this->Post);
		$map = array();
		if(!empty($like)) $map['like'] = $like;
		if(!empty($where)) $map['where'] = $where;
		$this->assign['result'] = $this->getPager($this->Post,$map);
		//echo $this->db->last_query();
		//exit;
		$this->assign['categories'] = self::$categories;
		$this->display();
	}
	
	function create() {
		if($this->input->post(null,true)){
			return $this->save();
		}else{
			$this->assign['categories'] = self::$categories;
			$this->display();
		}
	}
	
	function edit($id){
		if($this->input->post(null,true)){
			return $this->save('posts/edit');
		}else{
			$res = $this->Post->findByPk($id);
			if(empty($res)){
				return $this->showMessage('error_param',site_url('admin/posts'));
			}
			$this->assign['articles'] 	= $res[0];
			$this->assign['categories'] = self::$categories;
			$this->display();
		}
	}
	
	/**
	 * 保存文章
	 */
	private function save($type=''){
		$redirect = getRedirect(base_url('admin/posts'));
		
		$this->load->library('form_validation');
		$this->lang->load('template');
		
		if ($this->form_validation->run($type) == FALSE){
			return $this->showMessage(validation_errors()); 
		}
		$data = $this->input->post(null,true);
		if(($id = $this->Post->save($data['articles']))!==false){
			if (!empty($_FILES['logo']['name'])) {
				$fileId = empty($data['articles']['id']) ? $id : $data['articles']['id'];
				$upload = $this->uploadLogoFiles($fileId);
				if (!$upload['result']) {
					$mes = $upload['error'];
					return $this->showMessage($mes,$redirect);
				} 
				$article = array('id'=>$fileId, 'logo'=>$upload['file']);
				if (($ret = $this->Post->save($article)) == false) {
					return $this->showMessage($this->Post->_error);
				}
			}
			$mes = 'mes_success';
			return $this->showMessage($mes,$redirect);
		}else{
			return $this->showMessage($this->Post->_error); 
		}
	}
	
	/**
	 * 删除文章
	 * @param string $id
	 * @return bool
	 */
	public function remove($id=null){
		$redirect = getRedirect(base_url('admin/posts'));
		$id = $id>0?$id:intval($this->input->get('id',true));
		$res = $this->Post->findByPk($id);
		if(empty($res)){
			return $this->showMessage('error_param',site_url('admin/posts'));
		}
		if($this->Post->removeByPks($id) === true){
			if (isset($res[0]['logo'])) {
				$file = FCPATH.$res[0]['logo'];
				if (file_exists($file)) {
					@chmod( $file ,0777);
					@unlink($file);
				}
				$thumpFile = getThumbFile($file);
				if (file_exists($thumpFile)) {
					@chmod( $thumpFile ,0777);
					@unlink($thumpFile);
				}
			}
			$mes = 'mes_success';
			return $this->showMessage($mes,site_url('admin/posts'));
		}
		return $this->showMessage('failure',site_url('admin/posts'));
	}
}
/*End of file Posts.php*/
/*Location Posts.php */